<?php
/**
 * 产品上下架记录模型
 * @author sundongdong
 * @date 2016-07-09
 */
class Base_Model_Upshelf extends Model_Base {
	public function getUpshelfList(array $params) {
		$where = ' where 1';
		$filter = array();
		if ($params['goods_sns'] != '') {
			if (count(explode(',', $params['goods_sns'])) > 1){
				$where .= " and find_in_set(goods_sn, '{$params['goods_sns']}')";
			} else {
				$where .= " and goods_sn like :goods_sn";
				$filter['goods_sn'] = "%".$params['goods_sns']."%";
			}
		}
		if ($params['website'] != '') {
			$where .= " and web_site = :web_site";
			$filter['web_site'] = $params['website'];
		}
		if ($params['type'] != '') {
			$where .= " and type = :type";
			$filter['type'] = $params['type'];
		}
		if ($params['add_user'] != '') {
			$where .= " and add_user like :add_user";
			$filter['add_user'] = "%".$params['add_user']."%";
		}
		if ($params['start'] != '' || $params['end'] != '') {
			if ($params['start'] == '') {
				$where .= " and up_time <= :end";
				$filter['end'] = $params['end'].' 23:59:59';
			} elseif ($params['end'] == '') {
				$where .= " and up_time > :start";
				$filter['start'] = $params['start'];
			} else {
				$where .= " and up_time > :start and up_time <= :end";
				$filter['end'] = $params['end'] . ' 23:59:59';
				$filter['start'] = $params['start'];
			}
		}
		$sql = "select * from web_up_shelf ".$where ." order by id";
		$sql.= " limit ". ($params['page']-1) * $params['perPage'] .",".$params['perPage'];
		$countSql = "select count(1) from web_up_shelf ".$where;
		$data['list'] = $this->db()->fetchAll($sql, $filter);
		$data['count'] = $this->db()->fetchOne($countSql, $filter);
		foreach ($data['list'] as $key => $value) {
			$data['list'][$key]['add_user'] = $this->db()->fetchOne("select real_name from s_user where user_name = :name", array('name'=>$value['add_user']));
		}
		return $data;
	}
}